#include "lir.h"
#include "opcode.h"
#include <algorithm>
#include <cassert>

using namespace std;

Lir::Lir(LC t,Operand *d,Operand *a1,Operand *a2)
{
    type = t;
    dst = d;
    arg1 = a1;
    arg2 = a2;
}

void Lir::removeFromQueue(LirQueue &q)
{
    q.remove(this);
}

LirQueue::iterator LirQueue::getPosition(const Lir *l)
{
    return find(lirs.begin(),lirs.end(),l);
}

void LirQueue::add(Lir *l)
{
    lirs.push_back(l);
}

void LirQueue::insert(LirQueue::iterator it,Lir *l)
{
    lirs.insert(it,l);
}

void LirQueue::remove(Lir *l)
{
    lirs.remove(l);
}

void LirQueue::erase(LirQueue::iterator lit)
{
    lirs.erase(lit);
}

void LirQueue::clear()
{
    lirs.clear();
}

int LirQueue::distance(const Lir *l1,const Lir *l2)
{
    iterator pos1 = getPosition(l1);
    iterator pos2 = getPosition(l2);
    int d = ::distance(lirs.begin(),pos1) - ::distance(lirs.begin(),pos2);

    if(d < 0){
        d = -d;
    }
    return d;
}


